<template>
  <el-dialog
    :title="!dataForm.bid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="借款日期利率" prop="rate">
      <el-input v-model="dataForm.rate" placeholder="借款日期利率"></el-input>
    </el-form-item>
    <el-form-item label="借款期限" prop="term">
      <el-input v-model="dataForm.term" placeholder="借款期限"></el-input>
    </el-form-item>
    <el-form-item label="借款金额（元）" prop="jprice">
      <el-input v-model="dataForm.jprice" placeholder="借款金额（元）"></el-input>
    </el-form-item>
    <el-form-item label="出资金额（元）" prop="cprice">
      <el-input v-model="dataForm.cprice" placeholder="出资金额（元）"></el-input>
    </el-form-item>
    <el-form-item label="出资比率" prop="crate">
      <el-input v-model="dataForm.crate" placeholder="出资比率"></el-input>
    </el-form-item>
    <el-form-item label="预计收益" prop="income">
      <el-input v-model="dataForm.income" placeholder="预计收益"></el-input>
    </el-form-item>
    <el-form-item label="借款人电话" prop="uphone">
      <el-input v-model="dataForm.uphone" placeholder="借款人电话"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          bid: 0,
          rate: '',
          term: '',
          jprice: '',
          cprice: '',
          crate: '',
          income: '',
          uphone: ''
        },
        dataRule: {
          rate: [
            { required: true, message: '借款日期利率不能为空', trigger: 'blur' }
          ],
          term: [
            { required: true, message: '借款期限不能为空', trigger: 'blur' }
          ],
          jprice: [
            { required: true, message: '借款金额（元）不能为空', trigger: 'blur' }
          ],
          cprice: [
            { required: true, message: '出资金额（元）不能为空', trigger: 'blur' }
          ],
          crate: [
            { required: true, message: '出资比率不能为空', trigger: 'blur' }
          ],
          income: [
            { required: true, message: '预计收益不能为空', trigger: 'blur' }
          ],
          uphone: [
            { required: true, message: '借款人电话不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.bid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.bid) {
            this.$http({
              url: this.$http.adornUrl(`/user/butive/info/${this.dataForm.bid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.rate = data.butive.rate
                this.dataForm.term = data.butive.term
                this.dataForm.jprice = data.butive.jprice
                this.dataForm.cprice = data.butive.cprice
                this.dataForm.crate = data.butive.crate
                this.dataForm.income = data.butive.income
                this.dataForm.uphone = data.butive.uphone
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/butive/${!this.dataForm.bid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'bid': this.dataForm.bid || undefined,
                'rate': this.dataForm.rate,
                'term': this.dataForm.term,
                'jprice': this.dataForm.jprice,
                'cprice': this.dataForm.cprice,
                'crate': this.dataForm.crate,
                'income': this.dataForm.income,
                'uphone': this.dataForm.uphone
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
